/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const valid_map = ['admin', 'editor']
  return valid_map.indexOf(str.trim()) >= 0
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function validUrl(url) {
  // const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  const reg = /^(https?):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|top|vip|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str) {
  const reg = /^[a-z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str) {
  const reg = /^[A-Z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str) {
  const reg = /^[A-Za-z]+$/
  return reg.test(str)
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email) {
  const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return reg.test(email)
}

/**
 * @param {string} phone
 * @returns {boolean}
 */
export function validPhone(phone) {
  const reg = /^(\+86)?1\d{10}$/;
  return reg.test(phone);
}

/**
 * 校验固话格式
 * @param {string} landline
 * @returns {Boolean}
 */
export function validLandline(landline) {
  const reg = /^(\d{3,4}-?)?\d{8}$/;
  return reg.test(landline);
}

/**
 * 校验邮编
 * @param {string} postcode
 * @returns {Boolean}
 */
export function validPostcode(postcode) {
  const reg = /^\d{6}$/;
  return reg.test(postcode);
}

/**
 * 校验金额
 * @param amount
 * @returns {Boolean}
 */
export function validAmount(amount) {
  const reg = /^-?\d+(\.\d{1,2})?$/;
  return reg.test(amount);
}

/**
 * @param {string} num
 * @returns {Boolean}
 */
export function validNumber(num) {
  if(isNumber(num)) {
    return true;
  }
  const reg = /^(-?\d+)(\.\d+)?$/;
  if(reg.test(num)) {
    return true;
  }
  return false;
}

/**
 * @param {string} str
 * @returns {boolean}
 */
export function validLetter(str) {
  const reg = /^[A-Za-z]+$/;
  return reg.test(str);
}

/**
 * @param {string} integer
 * @returns {boolean}
 */
export function validInteger(integer) {
  if(Number.isInteger(integer)) {
    return true;
  }
  const reg = /^-?\d+$/;
  return reg.test(integer);
}

/**
 * 校验6到16位数字
 * @param {string} num
 * @returns {boolean}
 */
export function valid6To16Number(num) {
  num = "" + num;
  const reg = /^\d{6,16}$/;
  return reg.test(num);
}

/**
 * 校验6到18位数字
 * @param {string} str
 * @returns {boolean}
 */
export function valid6To18Letter(str) {
  if(typeof str === "undefined") {
    return false;
  }
  const reg = /^[A-Za-z]{6,18}$/;
  return reg.test(str);
}

/**
 * 校验6到16位任意字符
 * @param {string} str
 * @returns {boolean}
 */
export function valid6To16Char(str) {
  if(typeof str === "undefined") {
    return false;
  }
  const reg = /^.{6,16}$/;
  return reg.test(str);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str) {
  if (typeof str === 'string' || str instanceof String) {
    return true
  }
  return false
}

/**
 * @param {number} num
 * @returns {Boolean}
 */
export function isNumber(num) {
  return typeof num === 'number' && !isNaN(num);
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg) {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]'
  }
  return Array.isArray(arg)
}

/**
 * 校验值是否是空
 * @param val
 * @returns {boolean}
 */
export function isEmpty(val) {
  if(typeof val === "undefined" || val === null || val === "" || val === false) {
    return true;
  }
  if(val instanceof Array) {
    return val.length === 0;
  } else if(typeof val === "object") {
    return Object.keys(val).length === 0;
  }
  return false;
}

/**
 * 校验值是否是空
 * @param val
 * @returns {boolean}
 */
export function isBlank(val) {
  if(typeof val === "undefined" || val === null || val === "" || val === false) {
    return true;
  }
  if(typeof val === "string" || val instanceof String) {
    return val.trim() === "";
  } else if(val instanceof Array) {
    return val.length === 0;
  } else if(typeof val === "object") {
    return Object.keys(val).length === 0;
  }
  return false;
}
